package com.hxkj.service.impl;

import com.hxkj.domain.po.LoginToken;
import com.hxkj.domain.po.Token;
import com.hxkj.service.TokenService;
import com.hxkj.utils.TokenGenerator;
import com.platform.common.domain.result.ResultDataStruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TokenServiceImpl  implements TokenService{

    @Autowired
    private TokenGenerator tokenGenerator;

    @Override
    public ResultDataStruct<LoginToken> refresh(String token) throws Exception {
        ResultDataStruct<LoginToken> resultDataStruct = new ResultDataStruct<>();
        try {
            LoginToken loginToken = tokenGenerator.refreshToken(token);

            resultDataStruct.setCode(200);
            resultDataStruct.setMessage("成功");
            resultDataStruct.setData(loginToken);
        } catch (Exception e) {
            resultDataStruct.setCode(500);
            resultDataStruct.setMessage(e.getMessage());
        }
        return resultDataStruct;
    }

    @Override
    public ResultDataStruct<Token> validate(String accessToken) {
        ResultDataStruct<Token> resultDataStruct = new ResultDataStruct<>();

        try {
            Token token = tokenGenerator.validateToken(accessToken);
            resultDataStruct.setCode(200);
            resultDataStruct.setMessage("成功");
            resultDataStruct.setData(token);
        } catch (Exception e) {
            resultDataStruct.setCode(500);
            resultDataStruct.setMessage(e.getMessage());
        }
        return resultDataStruct;
    }
}
